﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
//using storeSMS.DBUtility;//Please add references
namespace storeSMS.DAL
{
	/// <summary>
	/// 数据访问类:reinfo
	/// </summary>
	public partial class reinfo
	{
		public reinfo()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("ID", "reinfo"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from reinfo");
			strSql.Append(" where ID="+ID+" ");
			return DbHelperOleDb.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
        public bool Add(storeSMS.Model.reinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.phone != null)
			{
				strSql1.Append("phone,");
				strSql2.Append("'"+model.phone+"',");
			}
			if (model.recontent != null)
			{
				strSql1.Append("recontent,");
				strSql2.Append("'"+model.recontent+"',");
			}
			if (model.rtime != null)
			{
				strSql1.Append("rtime,");
				strSql2.Append("'"+model.rtime+"',");
			}
			strSql.Append("insert into reinfo(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(storeSMS.Model.reinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update reinfo set ");
			if (model.phone != null)
			{
				strSql.Append("phone='"+model.phone+"',");
			}
			else
			{
				strSql.Append("phone= null ,");
			}
			if (model.recontent != null)
			{
				strSql.Append("recontent='"+model.recontent+"',");
			}
			else
			{
				strSql.Append("recontent= null ,");
			}
			if (model.rtime != null)
			{
				strSql.Append("rtime='"+model.rtime+"',");
			}
			else
			{
				strSql.Append("rtime= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where ID="+ model.ID+"");
			int rowsAffected=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from reinfo ");
			strSql.Append(" where ID="+ID+"" );
			int rowsAffected=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from reinfo ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public storeSMS.Model.reinfo GetModel(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  ");
			strSql.Append(" ID,phone,recontent,rtime ");
			strSql.Append(" from reinfo ");
			strSql.Append(" where ID="+ID+"" );
            storeSMS.Model.reinfo model = new storeSMS.Model.reinfo();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public storeSMS.Model.reinfo DataRowToModel(DataRow row)
		{
            storeSMS.Model.reinfo model = new storeSMS.Model.reinfo();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["phone"]!=null)
				{
					model.phone=row["phone"].ToString();
				}
				if(row["recontent"]!=null)
				{
					model.recontent=row["recontent"].ToString();
				}
				if(row["rtime"]!=null && row["rtime"].ToString()!="")
				{
					model.rtime=DateTime.Parse(row["rtime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("SELECT storeinfo.gys, storeinfo.project, storeinfo.projectbh, storeinfo.cgddh, storeinfo.qr, storeinfo.gh, storeinfo.nums, reinfo.phone, reinfo.ID,reinfo.recontent, reinfo.rtime FROM storeinfo RIGHT JOIN reinfo ON storeinfo.phone = reinfo.phone ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where 1=1 "+strWhere);
			}
            strSql.Append(" order by rtime desc ");
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM reinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from reinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

